-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Marshal empty as null #9
Marshal empty as null #9
Conversation
This change adds support for marshaling and unmarshaling “null” and reflecting that the value is not present, rather than relying on the “zero” value
@mwielbut thanks for the PR! I agree this makes sense. I'll create a new minor release and note that it's a breaking change. Would you mind removing the go mod files from the PR? I haven't yet switched the project over from dep but am planning to soon. |
Awesome! Just removed the |
Cheers! Thanks again! |
* Support “null” This change adds support for marshaling and unmarshaling “null” and reflecting that the value is not present, rather than relying on the “zero” value * Fix to specify {{ .VariableName }} * Add support for go modules * Fix go mod path * Update golden test * Remove go.mod per pull request
* Marshal empty as null (#9) * Support “null” This change adds support for marshaling and unmarshaling “null” and reflecting that the value is not present, rather than relying on the “zero” value * Fix to specify {{ .VariableName }} * Add support for go modules * Fix go mod path * Update golden test * Remove go.mod per pull request * Add example test * Update README.md * MustGet: Add ability to get value and panic if not present Co-authored-by: Openly, Inc <openlyinsured@users.noreply.github.com> Co-authored-by: Mateusz Wielbut <mwielbut@users.noreply.github.com> Co-authored-by: Matt Wielbut <matt@openly.com>
The current implementation marshals non-present values as their zero value. When the zero values are read back in, we lose the fidelity of knowing that the value was actually not-present vs. explicitly set to the zero value (the whole purpose of using the optional library). I don't think this should be expected outcome.
This pull request proposes a modification to the
UnmarshalJSON
andMarshalJSON
functions, marshaling JSONnull
when the value isnil
, and unmarshaling JSONnull
tonil
. This seems to be more in-line with what should be expected when writing this out to JSON.Note: this is a breaking change for dependents that rely on the existing functionality of marshaling to the zero value.
Thanks!